#!/bin/bash

# jenkins home
jenkins_home=/home/jenkins

if [ ! -d ${jenkins_home} ]; then
	UUID=$(mdata-get sdc:uuid)
	DDS=zones/${UUID}/data

	if zfs list ${DDS} 1>/dev/null 2>&1; then
		# Create delegate dataset for home/admin
		zfs create -p ${DDS}${jenkins_home}		

		# Set mount point and fix permissions
		zfs set mountpoint=${jenkins_home} ${DDS}${jenkins_home}		

		# znapzend for backup
		znapzendzetup create --recursive --tsformat='%Y-%m-%d-%H%M%S' --donotask \
			SRC '7day=>8hour,30day=>1day,1year=>1week,10year=>1month' ${DDS}
		/usr/sbin/svcadm enable svc:/pkgsrc/znapzend:default
	else
		mkdir -p ${jenkins_home}
	fi
fi

chown -R jenkins:jenkins ${jenkins_home}